package com.yuji.aacar.controller.order.detail;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.Const;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.bean.response.OrderDetailResponse;
import com.yuji.aacar.controller.order.rebuy.OrderDetailRebuyActivity;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.fres.CoreImageLoader;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.utils.TimeUtils;
import com.yuji.aacar.widgets.autolayout.AutoLinearLayout;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.ratingbar.MyRatingBar;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.order_detail_number)
    PingFangTextView orderDetailNumber;
    @BindView(R.id.order_detail_status)
    PingFangTextView orderDetailStatus;
    @BindView(R.id.order_detail_iv)
    ImageView orderDetailIv;
    @BindView(R.id.order_detail_product_name)
    PingFangTextView orderDetailProductName;
    @BindView(R.id.order_detail_product_detail)
    PingFangTextView orderDetailProductDetail;
    @BindView(R.id.order_comment_product_price)
    PingFangTextView orderCommentProductPrice;
    @BindView(R.id.order_comment_price_comment)
    TextView orderCommentPriceComment;
    @BindView(R.id.order_detail_but_again_btn)
    Button orderDetailButAgainBtn;
    @BindView(R.id.order_detail_name)
    PingFangTextView orderDetailName;
    @BindView(R.id.order_detail_car)
    PingFangTextView orderDetailCar;
    @BindView(R.id.order_detail_address)
    PingFangTextView orderDetailAddress;
    @BindView(R.id.order_detail_booktime)
    PingFangTextView orderDetailBooktime;
    @BindView(R.id.detail_comment)
    PingFangTextView detailComment;
    @BindView(R.id.detail_price)
    PingFangTextView detailPrice;
    @BindView(R.id.detail_coupon)
    PingFangTextView detailCoupon;
    @BindView(R.id.detail_jifen)
    PingFangTextView detailJifen;
    @BindView(R.id.detail_discount)
    PingFangTextView detailDiscount;
    @BindView(R.id.detail_total)
    PingFangTextView detailTotal;
    @BindView(R.id.order_success_head_iv)
    SimpleDraweeView orderSuccessHeadIv;
    @BindView(R.id.order_detail_worker_name)
    PingFangTextView orderDetailWorkerName;
    @BindView(R.id.order_detail_call)
    AutoRelativeLayout orderDetailCall;
    @BindView(R.id.order_detail_worker_layout)
    AutoLinearLayout orderDetailWorkerLayout;
    @BindView(R.id.order_detail_layout1)
    AutoLinearLayout orderDetailLayout1;
    @BindView(R.id.order_detail_service_ratingbar)
    MyRatingBar orderDetailServiceRatingbar;
    @BindView(R.id.order_detail_layout2)
    AutoLinearLayout orderDetailLayout2;
    @BindView(R.id.order_detail_clean_ratingbar)
    MyRatingBar orderDetailCleanRatingbar;
    @BindView(R.id.order_detail_suggestion1)
    AutoRelativeLayout orderDetailSuggestion1;
    @BindView(R.id.order_detail_suggestion2)
    AutoRelativeLayout orderDetailSuggestion2;
    @BindView(R.id.order_detail_suggestion3)
    AutoRelativeLayout orderDetailSuggestion3;
    @BindView(R.id.order_detail_suggestion4)
    AutoRelativeLayout orderDetailSuggestion4;
    @BindView(R.id.order_detail_comment_str)
    EditText orderDetailCommentStr;
    @BindView(R.id.order_detail_submit_btn)
    Button orderDetailSubmitBtn;
    @BindView(R.id.order_detail_comment_all_layout)
    AutoLinearLayout orderDetailCommentAllLayout;
    @BindView(R.id.order_detail_comment)
    PingFangTextView orderDetailComment;
    @BindView(R.id.order_cancel)
    Button orderCancel;

    private String orderNumber;
    private int washType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        initTitle(true, "订单详情", null);
        super.onCreate(savedInstanceState);

        initUI();
    }

    private void initUI() {
        orderNumber = getIntent().getStringExtra("orderNo");
        washType = getIntent().getIntExtra("washType", 0);
        //        orderDetailCommentAllLayout.setVisibility(View.GONE); //订单评价
        LogUtil.e("--initUI--", orderNumber);
        LogUtil.e("--initUI--", washType);
        orderDetailNumber.setText("订单号:" + orderNumber);
        if (washType == Const.WASH_TYPE_RESERVE) {
            orderDetailIv.setImageResource(R.mipmap.icon_reserve);
        } else {
            orderDetailIv.setImageResource(R.mipmap.icon_immediate);
        }
    }


    private void requestOrderDetail() {
        requestParams = BaseServiceParams.detail(orderNumber);
        doTask(BaseServiceMediator.DETAIL);
    }


    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();
        //        requestParams =
        requestOrderDetail();
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.DETAIL_CODE) {
            OrderDetailResponse submitResponse = (OrderDetailResponse) response.getResponse();
            resetUI(submitResponse);
        } else if (response.getRequestCode() == BaseServiceMediator.CANCEL_CODE) {
            showToast("订单已经取消！");
            CoreEventCenter.postMessage(EventConst.BACK_FROM_ORDER_DETAIL, "refresh");
            finish();
        }
    }

    private void resetUI(OrderDetailResponse submitResponse) {
        String orderStatusName = submitResponse.getOrderStautsName();
        String orderStatus = submitResponse.getOrderStauts();
        orderDetailStatus.setText(orderStatusName);
        if (orderStatus.contentEquals(Const.ORDER_STATUS_SHOULD_PAY) || orderStatus.contentEquals(Const.ORDER_STATUS_SHOULD_CONFIRM)) {
            orderCancel.setVisibility(View.VISIBLE);
        } else if (orderStatus.contentEquals(Const.ORDER_STATUS_CONFIRM) || orderStatus.contentEquals(Const.ORDER_STATUS_WASHING)) {
            orderDetailWorkerLayout.setVisibility(View.VISIBLE);
            CoreImageLoader.loadImage(orderSuccessHeadIv, submitResponse.getWasherHeadUrl());
            orderDetailWorkerName.setText(submitResponse.getWasherName());
            orderDetailCall.setTag(submitResponse.getWasherPhoneNo());
        } else if (orderStatus.contentEquals(Const.ORDER_STATUS_FINISH)) {
            orderDetailWorkerLayout.setVisibility(View.GONE);
        }

        orderDetailName.setText(submitResponse.getLinkman() + " " + submitResponse.getPhoneNo());
        orderDetailCar.setText(submitResponse.getBrandName() + "  " + submitResponse.getSeriesName() + "   " + submitResponse.getCarNo());
        orderDetailAddress.setText(submitResponse.getCityName() + " " + submitResponse.getAreaName() + " " + submitResponse.getRoomName() + " " + submitResponse.getDetailAddress());
        orderDetailBooktime.setText(TimeUtils.stampToDate(submitResponse.getBookStartTime()) + " " + TimeUtils.stampToDate(submitResponse.getBookEndTime()).split(" ")[1]);


        String userRank = submitResponse.getUserRankId();
        String productPriceStr = null;
        if (null != userRank) {
            int rank = Integer.parseInt(userRank);
            if (rank > 1) {
                detailComment.setText("会员价格");

                productPriceStr = submitResponse.getResultProductPrice();
                if (null != productPriceStr) {
                    detailPrice.setText("¥" + productPriceStr);
                }
            } else {
                productPriceStr = submitResponse.getProductPrice();
                if (null != productPriceStr) {
                    detailPrice.setText("¥" + productPriceStr);
                }
            }
        }


        String titlePriceStr = submitResponse.getPayAmount();
        if (null != titlePriceStr) {
            float titlePrice = Float.parseFloat(titlePriceStr);
            titlePrice = (float) (Math.round(titlePrice * 100)) / 100;
            orderCommentProductPrice.setText("¥" + titlePrice);
            detailTotal.setText("¥" + titlePrice);
        }

        //        orderCommentProductPrice.setText("¥" + submitResponse.getPayAmount());
        //        orderCommentPriceComment.setText("已优惠" + submitResponse.getDiscAmount() + "元");


        String couponStr = submitResponse.getCouponAmount();
        if (null != couponStr) {
            float coupon = Float.parseFloat(couponStr);
            coupon = (float) (Math.round(coupon * 100)) / 100;
            if (null != productPriceStr && !TextUtils.isEmpty(productPriceStr)) {
                float productPrice = Float.parseFloat(productPriceStr);
                productPrice = (float) (Math.round(productPrice * 100)) / 100;
                if (coupon > productPrice) {
                    coupon = productPrice;
                }
            }
            detailCoupon.setText("-¥" + coupon);
        }

        String jifenStr = submitResponse.getPayIntegral();
        if (null != jifenStr) {
            float jifen = Float.parseFloat(jifenStr);
            jifen = (float) (Math.round(jifen * 100)) / 100;
            detailJifen.setText("-¥" + jifen);
        }

        String accountStr = submitResponse.getBookDiscPrice();
        if (null != accountStr) {
            float account = Float.parseFloat(accountStr);
            account = (float) (Math.round(account * 100)) / 100;
            detailDiscount.setText("-¥" + account);
        }

    }

    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @OnClick({R.id.order_detail_submit_btn, R.id.order_cancel, R.id.order_detail_call})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_detail_submit_btn:
                presentController(OrderDetailRebuyActivity.class);
                break;
            case R.id.order_cancel:

                DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        cancelOrder();
                    }
                };
                showAlertDialogue("取消", "取消订单", "会在7个工作日左右退还给您", "确定", clickListener);


                break;
            //            case R.id.order_detail_sendmessage:
            //                String phone = (String) view.getTag();
            //                Tools.sendSMSTo(OrderDetailActivity.this, phone, "");
            //                break;
            case R.id.order_detail_call:
                String phoneNo = (String) view.getTag();
                Tools.diallPhone(OrderDetailActivity.this, phoneNo);
                break;
        }
    }

    private void cancelOrder() {
        requestParams = BaseServiceParams.cancel(orderNumber);
        doTask(BaseServiceMediator.CANCEL);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        CoreEventCenter.postMessage(EventConst.BACK_FROM_ORDER_DETAIL, "refresh");
    }
}